/*
 * Copyright (c) 2014. www.emoney.cn
 * Author: LiuYuguang.
 * 2014/9/30
 */

package com.shcz.migu.widget.base;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shcz.migu.R;


public class CTitleBar extends FrameLayout {
    public static final int BUTTON_LEFT_ICON = 0;
    public static final int BUTTON_TITLE_TEXT = 1;
    public static final int BUTTON_RIGHT_ICON1 = 2;
    public static final int BUTTON_RIGHT_ICON2 = 3;

    public static final int NET_ERR_NO_NETWORK = 0;
    public static final int NET_ERR_BAD_NETWORK = 1;

    private ProgressBar mProgressBar;

    private View titleBar;

    private View titleBarText;

    private static final String TAG = "CTitleBar";

    private static long CLICK_TIME = System.currentTimeMillis();
    private static final long CLICK_DURATION = 200;


    private OnTitleButtonClickListener mListener = null;

    private View mCustomTitleView = null;


    public CTitleBar(Context context) {
        super(context);
        init(null);
    }

    public CTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public CTitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        Context cntx = getContext();
        LayoutInflater inflater = (LayoutInflater) cntx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.app_widget_title_bar, this);
        mProgressBar = (ProgressBar) findViewById(R.id.widget_title_bar_progress_bar);

        if (attrs != null) {
            TypedArray a = cntx.obtainStyledAttributes(attrs, R.styleable.CTitleBar);
            int icn = a.getResourceId(R.styleable.CTitleBar_leftIcn, -1);
            setIconButton(icn, R.id.widget_title_bar_icn_l);

            icn = a.getResourceId(R.styleable.CTitleBar_rightIcn1, -1);
            setIconButton(icn, R.id.widget_title_bar_icn_r1);
            icn = a.getResourceId(R.styleable.CTitleBar_rightIcn2, -1);
            setIconButton(icn, R.id.widget_title_bar_icn_r2);

            TextView textView = (TextView) findViewById(R.id.widget_title_bar_title_txt);
            textView.setText(a.getString(R.styleable.CTitleBar_titleTxt));
            icn = a.getResourceId(R.styleable.CTitleBar_titleIcn, -1);
            setIconButton(BUTTON_TITLE_TEXT, icn);

            a.recycle();
        }

        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    long currentTime = System.currentTimeMillis();
                    if ((currentTime - CLICK_TIME) <= CLICK_DURATION) {
                        CLICK_TIME = currentTime;
                        return;
                    }
                    CLICK_TIME = currentTime;
                    switch (view.getId()) {
                        case R.id.widget_title_bar_icn_l:
                            mListener.onTitleButtonClicked(BUTTON_LEFT_ICON);
                            break;
                        case R.id.widget_title_bar_title_txt:
                            mListener.onTitleButtonClicked(BUTTON_TITLE_TEXT);
                            break;
                        case R.id.widget_title_bar_title_icn:
                            mListener.onTitleButtonClicked(BUTTON_TITLE_TEXT);
                            break;
                        case R.id.widget_title_bar_icn_r1:
                            mListener.onTitleButtonClicked(BUTTON_RIGHT_ICON1);
                            break;
                        case R.id.widget_title_bar_icn_r2:
                            mListener.onTitleButtonClicked(BUTTON_RIGHT_ICON2);
                            break;
                    }
                }
            }
        };
        int ids[] = {R.id.widget_title_bar_icn_l, R.id.widget_title_bar_title_txt,
                R.id.widget_title_bar_title_icn, R.id.widget_title_bar_icn_r1, R.id.widget_title_bar_icn_r2};
        for (int id : ids) {
            View v = findViewById(id);
            v.setOnClickListener(listener);
        }
        setClickable(true);

        applyTheme();
    }

    public void applyTheme() {
        Resources res = getResources();
        titleBar = findViewById(R.id.widget_title_bar);
        titleBar.setBackgroundColor(res.getColor(R.color.title_bar_color));

        View v = findViewById(R.id.widget_title_bar_progress_bar);
//        ((ProgressBar)v).setIndeterminateDrawable(res.getDrawable(ResourceUtils.getResourceByName(Theme.CTitleBar.progress_bar)));
        titleBarText = findViewById(R.id.widget_title_bar_title_txt);
        ((TextView) titleBarText).setTextColor(res.getColor(R.color.widget_title_bar_title_txt_clr));
        v = findViewById(R.id.widget_title_bar_net_err_notifier);
        v.setBackgroundColor(res.getColor(R.color.widget_title_bar_net_err_notifier_bg));
        v = findViewById(R.id.widget_title_bar_net_err_text);
        ((TextView) v).setTextColor(res.getColor(R.color.widget_title_bar_net_err_text_clr));
        findViewById(R.id.widget_title_bar_bottom_line).setBackgroundColor(res.getColor(R.color.widget_title_bar_bottom_divier_clr));
    }

    public void setTitleBarBackground(int color) {
        titleBar.setBackgroundColor(color);
    }

    public void setTitleBarDividerBackground(int color) {
        findViewById(R.id.widget_title_bar_bottom_line).setBackgroundColor(color);
    }

    public void setTitleBarDividerVisibility(int visibility) {
        findViewById(R.id.widget_title_bar_bottom_line).setVisibility(visibility);
    }

    public void setTitleBarTextColor(int color) {
        TextView textView = (TextView) titleBarText;
        textView.setTextColor(color);
    }

    public ProgressBar getProgressBar() {
        if (mProgressBar == null) {
            mProgressBar = (ProgressBar) findViewById(R.id.widget_title_bar_progress_bar);
        }
        return mProgressBar;
    }

    private void setIconButton(int drawable, int image) {
//        ImageView imageView = (ImageView) findViewById(image);
        ImageViewExtends imageView = (ImageViewExtends) findViewById(image);
        if (imageView == null) {
            return;
        }
        if (drawable == -1) {
//            imageView.setVisibility(View.GONE);
            return;
        }
        try {
            Resources res = getResources();
            String resType = res.getResourceTypeName(drawable);
            if ("drawable".equals(resType)) {
                imageView.setImageResource(drawable);
                imageView.setVisibility(View.VISIBLE);
                return;
            }
        } catch (Resources.NotFoundException ex) {
            //comment in batches by zxt  e.print StackTrace();
        } catch (UnsupportedOperationException ex) {
            //comment in batches by zxt  e.print StackTrace();
        }
        imageView.setVisibility(View.GONE);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        p.gravity = Gravity.CENTER_VERTICAL;
        return p;
    }

    public void setTitleRightText(CharSequence text, int color, OnClickListener listener) {
        TextView textView = (TextView) findViewById(R.id.widget_title_bar_right_txt);
        textView.setVisibility(VISIBLE);
        if (textView != null) {
            textView.setText(text);
            textView.setTextColor(color);
            if (listener != null) textView.setOnClickListener(listener);
        }
    }

    public void setTitleRightText(CharSequence text, OnClickListener listener) {
        TextView textView = (TextView) findViewById(R.id.widget_title_bar_right_txt);
        textView.setVisibility(VISIBLE);
        if (textView != null) {
            textView.setText(text);
            if (listener != null) textView.setOnClickListener(listener);
        }
    }

    public void setTitleRightText(CharSequence text) {
        TextView textView = (TextView) findViewById(R.id.widget_title_bar_right_txt);
        textView.setVisibility(VISIBLE);
        textView.setText(text);
    }
    public void setTitleLeftText(CharSequence text, int color, OnClickListener listener){
        TextView textView = (TextView) findViewById(R.id.widget_title_bar_left_txt);
        textView.setVisibility(VISIBLE);
        textView.setTextColor(color);
        if (textView!=null){
            textView.setText(text);
            if (listener!=null)textView.setOnClickListener(listener);
        }
    }

    public void setTitle(CharSequence title) {
        TextView textView = (TextView) findViewById(R.id.widget_title_bar_title_txt);
        if (textView != null) {
            textView.setText(title);
        }
    }

    public void setTitleTextColor(int color) {
        TextView textView = (TextView) findViewById(R.id.widget_title_bar_title_txt);
        if (textView != null) {
            textView.setTextColor(color);
        }
    }

    public void setTitleLeftButtonVisibility(int visibility) {
        ImageView imageView = (ImageView) findViewById(R.id.widget_title_bar_icn_l);
        imageView.setVisibility(visibility);
    }
    public void setTitleLeftButton( OnClickListener listener) {
        ImageView imageView = (ImageView) findViewById(R.id.widget_title_bar_icn_l);
        if (listener != null) imageView.setOnClickListener(listener);
        imageView.setVisibility(VISIBLE);
    }

    public void setTitleLeftButton(int resId, OnClickListener listener) {
        ImageView imageView = (ImageView) findViewById(R.id.widget_title_bar_icn_l);
        imageView.setImageResource(resId);
        if (listener != null) imageView.setOnClickListener(listener);
        imageView.setVisibility(VISIBLE);
    }

    public void setTitleRightButtonVisibility(int visibility) {
        ImageView imageView = (ImageView) findViewById(R.id.widget_title_bar_icn_r2);
        imageView.setVisibility(visibility);
    }

    public void setTitleRightButton(int resId, OnClickListener listener) {
        ImageView imageView = (ImageView) findViewById(R.id.widget_title_bar_icn_r2);
        imageView.setImageResource(resId);
        if (listener != null) imageView.setOnClickListener(listener);
        imageView.setVisibility(VISIBLE);
    }



    public void setIcon(int buttonType, int drawable) {
        switch (buttonType) {
            case BUTTON_LEFT_ICON:
                setIconButton(drawable, R.id.widget_title_bar_icn_l);
                break;
            case BUTTON_RIGHT_ICON1:
                setIconButton(drawable, R.id.widget_title_bar_icn_r1);
                break;
            case BUTTON_RIGHT_ICON2:
                setIconButton(drawable, R.id.widget_title_bar_icn_r2);
                break;
            case BUTTON_TITLE_TEXT:
                setIconButton(drawable, R.id.widget_title_bar_title_icn);
                break;
        }

    }

    public void setIcon(int buttonType, Drawable drawable) {
        int id = -1;
        switch (buttonType) {
            case BUTTON_LEFT_ICON:
                id = R.id.widget_title_bar_icn_l;
                break;
            case BUTTON_RIGHT_ICON1:
                id = R.id.widget_title_bar_icn_r1;
                break;
            case BUTTON_RIGHT_ICON2:
                id = R.id.widget_title_bar_icn_r2;
                break;
            case BUTTON_TITLE_TEXT:
                id = R.id.widget_title_bar_title_icn;
                break;
        }
        if (id != -1) {
            ImageView imageView = (ImageView) findViewById(id);
            if (drawable != null) {
                imageView.setImageDrawable(drawable);
                imageView.setVisibility(View.VISIBLE);
            } else {
                imageView.setVisibility(View.GONE);
            }
        }

    }

    /*
    一次性设置标题栏中所有的图标。参数含义与资源属性一致。非法的资源值表示隐藏该图标。
    */
    public void setIcons(int leftIcn, int titleIcn, int rightIcn1, int rightIcn2) {
        setIcon(BUTTON_LEFT_ICON, leftIcn);
        setIcon(BUTTON_TITLE_TEXT, titleIcn);
        setIcon(BUTTON_RIGHT_ICON1, rightIcn1);
        setIcon(BUTTON_RIGHT_ICON2, rightIcn2);
    }

    public void setOnClickListener(OnTitleButtonClickListener listener) {
        mListener = listener;
    }

    @Override
    public boolean isInEditMode() {
        return true;
    }

    public void customizeCenterArea(View view) {
        FrameLayout area = (FrameLayout) findViewById(R.id.widget_title_bar_center_area);
        area.removeAllViews();
        area.addView(view);
    }

    public void setCustomTitleView(View view) {
        ;
        FrameLayout area = (FrameLayout) findViewById(R.id.widget_title_bar_center_area);
        if (view != null) {
            area.findViewById(R.id.widget_title_bar_title_txt).setVisibility(View.GONE);
            if (mCustomTitleView != null) {
                area.removeView(mCustomTitleView);
                mCustomTitleView = null;
            }
            area.addView(view);
            mCustomTitleView = view;
        } else {
            area.findViewById(R.id.widget_title_bar_title_txt).setVisibility(View.VISIBLE);
            if (mCustomTitleView != null) {
                area.removeView(mCustomTitleView);
                mCustomTitleView = null;
            }
        }
    }

    public void customizeRightArea(View view) {
        LinearLayout area = (LinearLayout) findViewById(R.id.widget_title_bar_right_area);
        area.removeAllViews();
        area.addView(view);
    }

    public void customizeLeftArea(View view) {

        LinearLayout area = (LinearLayout) findViewById(R.id.widget_title_bar_left_area);
        area.removeAllViews();
        area.addView(view);
    }

    public void showNetErrorNotification(int status, OnClickListener listener) {
        TextView txt = (TextView) findViewById(R.id.widget_title_bar_net_err_text);
        switch (status) {
            case NET_ERR_NO_NETWORK:
//                txt.setText(R.string.network_conn_error);
                break;
            case NET_ERR_BAD_NETWORK:
//                txt.setText(R.string.app_notify_network_hint_bad);
                break;
        }
        ;
        View v = findViewById(R.id.widget_title_bar_net_err_notifier);
        v.setVisibility(View.VISIBLE);
        v.setOnClickListener(listener);
    }

    public void hideNetErrorNofitification() {
        findViewById(R.id.widget_title_bar_net_err_notifier).setVisibility(View.GONE);
    }

    public interface OnTitleButtonClickListener {
        /*
        说明：通知事件处理方哪个标题栏按钮被点击。
        参数：
            buttonType  用户点击的按钮类型。该参数值可能为：
                BUTTON_LEFT_ICON    左侧按钮
                BUTTON_TITLE_TEXT   中间标题文字及图标
                BUTTON_RIGHT_ICON1  右侧第一个图标
                BUTTON_RIGHT_ICON2  右侧第二个图标
         */
        public void onTitleButtonClicked(int buttonType);
    }

    public interface OnTitleTabClickListener {

        public void onTabLeftClicked();

        public void onTabRightClicked();
    }

}
